// Copyright (c) 2011 The Kiwii Authors. All rights reserved.
// Use of this source code is governed by the New BSD License that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include "backend/KiwiiClient.hxx"
#include "comm/Protocol.hxx"

namespace kiwii {

  KClient::KClient(int fd, net::EventLoopBase& el) 
    : conn_(fd, el) {
    conn_.setMessageHandler([this](char const* buf, size_t size)->size_t {
	if (size == 0) {
	  //The client end has closed the connection, so kill me
	  delete this;
	}

	comm::protocol::Request req(buf, size);
	if (req.ok()) {
	  onRequest(req);
	}
	return req.bytes();
      });
  }

}
